const fs = require("fs");
const path = require("path");

/**
 * 创建一个写入文件流
 * @param {*} fileName
 * @returns
 */
function createWriteStream(fileName) {
  // 创建读取文件流
  const fullFileName = path.join(__dirname, "../", "../", "logs", fileName);
  const writeStream = fs.createWriteStream(fullFileName, {
    flags: "a", // 追加写入
  });
  return writeStream;
}

// 写日志
function writeLog(writeStream, log) {
  writeStream.write(log + "\n"); // 关键代码
}

// 写入访问日志
const accessWriteStream = createWriteStream("access.log");
function access(log) {
  writeLog(accessWriteStream, log);
}

module.exports = {
  access,
};
